package com.zx._03_并发.chapter_01_atomic;

import com.zx._03_并发.entity.Counter;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import lombok.extern.slf4j.Slf4j;

/**
 * 线程安全——
 */
@Slf4j
public class Demo_05 {

    private static AtomicIntegerFieldUpdater<Counter> updater =
            AtomicIntegerFieldUpdater.newUpdater(Counter.class, "count");


    public static void main(String[] args) {
        Counter counter = new Counter();

        counter.count = 100;

        if (updater.compareAndSet(counter, 100, 120)) {
            log.info("update success 1, {}", counter.count);
        }

        if (updater.compareAndSet(counter, 100, 120)) {
            log.info("update success 2, {}", counter.count);
        } else {
            log.info("update failed, {}", counter.count);
        }
    }

}
